<?php
namespace app\admin\controller;
use think\Db;
use lib\Curl;
class News extends Base{
    //广告图
    public function news_banner(){
        if(request()->isGet()){
            $param = input('get.');
            $data=Db::name('news_banner')->where('id',1)->find();
            $this->assign('data',$data);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['banner']=$param['banner'];
            $res= Db::name('news_banner')->where(array('id'=>1))->update($data);
            if($res){
                return json(['code' => 200,'msg' => '更新成功']);
            }else{
                return json(['code' => 500,'msg' => '失败']);
            }
        }
    }

    public function news_cate(){
        $list=Db::name('news_cate')->where('state','<',3)->order('id desc')->paginate(10);
        $counts=Db::name('news_cate')->where('state','<',3)->count();
        $data=$list->all();
        $this->assign('data',$data);
        $this->assign('list',$list);
        $this->assign('counts',$counts);
        return $this->fetch();
    }
    public function news_cate_add(){
        if(request()->isGet()){
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['title']=$param['title'];
            $data['img']=$param['img'];
            $data['description']=$param['description'];
            $data['state']=$param['state'];
            $data['sort']=$param['sort'];
            $res= Db::name('news_cate')->insertGetId($data);
            if($res){
                return json(['code' => 200, 'msg' => '成功！']);
            }else{
                return json(['code' => 500, 'msg' => '失败！']);
            }
        }
    }
    public function news_cate_edit(){
        if(request()->isGet()){
            $param = input('get.');
            $data=Db::name('news_cate')->where('id',$param['id'])->find();
            $this->assign('data',$data);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['title']=$param['title'];
            $data['img']=$param['img'];
            $data['description']=$param['description'];
            $data['state']=$param['state'];
            $data['sort']=$param['sort'];
            $res=Db::name('news_cate')->where(array('id'=>$param['id']))->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '成功！']);
            }else{
                return json(['code' => 500, 'msg' => '失败！']);
            }
        }
    }
    public function news_cate_del(){
        $param = input('post.');
        $res= Db::name('news_cate')->where(array('id'=>$param['id']))->update(['state'=>3]);
        return json(['code' => 200,'msg' => '删除成功']);

    }
    public function cate_state_off(){
        $id=input('param.id');
        $status = Db::name('news_cate')->where(array('id'=>$id))->update(['state'=>2]);
        if($status){
            return json(['code' => 200,'msg' => '已禁止']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function cate_state_on(){
        $id=input('param.id');
        $status = Db::name('news_cate')->where(array('id'=>$id))->update(['state'=>1]);
        if($status){
            return json(['code' => 200,'msg' => '已开启']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function news_list(){
        $keyword=input('get.keyword');
        $state=input('get.state');
        $c_id=input('get.c_id');
        if (!empty(input('get.fromtime'))) {
            $fromtime=strtotime(input('get.fromtime'));
        }else{
            $fromtime=0;
        }
        if (!empty(input('get.totime'))) {
            $totime=strtotime(input('get.totime'))+86400;
        }else{
            $totime=time();
        }
        $map = [
            ['a.time', '>', $fromtime],
            ['a.time', '<', $totime]
        ];
        if($state==0){
            array_push($map,['a.state','<',3]);
        }else{
            array_push($map,['a.state','=',$state]);
        }
        if($c_id>0){
            array_push($map,['a.c_id','=',$c_id]);
        }
        // $list=Db::name('news')->where($map)->where('title','like',"%{$keyword}%")->order('id desc')->paginate(10);
        // $counts=Db::name('news')->where($map)->where('title','like',"%{$keyword}%")->count();
        $list=Db::name('news')->alias('a')->join('news_cate b', 'a.c_id = b.id','LEFT')->field('a.*,b.title as cate_name')->where($map)->where('b.state',1)->where('a.title','like',"%{$keyword}%")->order('a.id desc')->paginate(10);
        $counts=Db::name('news')->alias('a')->join('news_cate b', 'a.c_id = b.id','LEFT')->field('a.*,b.title as cate_name')->where($map)->where('b.state',1)->where('a.title','like',"%{$keyword}%")->count();
        $data=$list->all();
        $news_cate=Db::name('news_cate')->where('state',1)->order('id asc')->select();
        $this->assign('news_cate',$news_cate);
        $this->assign('keyword',input('get.keyword'));
        $this->assign('fromtime',input('get.fromtime'));
        $this->assign('totime',input('get.totime'));
        $this->assign('state',$state);
        $this->assign('c_id',$c_id);
        $this->assign('data',$data);
        $this->assign('list',$list);
        $this->assign('counts',$counts);
        return $this->fetch();
    }
    public function news_add(){
        if(request()->isGet()){
            $news_cate=Db::name('news_cate')->where('state',1)->order('id asc')->select();
            $this->assign('pubtime',date('Y-m-d',time()));
            $this->assign('news_cate',$news_cate);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['c_id']=$param['c_id'];
            $data['title']=$param['title'];
            $data['author']=$param['author'];
            $data['views']=$param['views'];
            $data['detail']=html_entity_decode($param['detail']);
            $data['img']=$param['img'];
            $data['seo_keywords']=$param['seo_keywords'];
            $data['seo_description']=$param['seo_description'];
            $data['sort']=$param['sort'];
            $data['state']=$param['state'];
            $data['pubtime']=strtotime($param['pubtime']);
            $data['time']=time();
            $data['uptime']=time();
            $res= Db::name('news')->insertGetId($data);
            if($res){
                return json(['code' => 200, 'msg' => '成功！']);
            }else{
                return json(['code' => 500, 'msg' => '失败！']);
            }
        }
    }
    public function news_edit(){
        if(request()->isGet()){
            $param = input('get.');
            $data=Db::name('news')->where('id',$param['id'])->find();
            $news_cate=Db::name('news_cate')->where('state',1)->order('id asc')->select();
            $this->assign('news_cate',$news_cate);
            $this->assign('data',$data);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['c_id']=$param['c_id'];
            $data['title']=$param['title'];
            $data['author']=$param['author'];
            $data['views']=$param['views'];
            $data['detail']=html_entity_decode($param['detail']);
            $data['img']=$param['img'];
            $data['seo_keywords']=$param['seo_keywords'];
            $data['seo_description']=$param['seo_description'];
            $data['sort']=$param['sort'];
            $data['state']=$param['state'];
            $data['pubtime']=strtotime($param['pubtime']);
            $data['uptime']=time();
            $res=Db::name('news')->where(array('id'=>$param['id']))->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '成功！']);
            }else{
                return json(['code' => 500, 'msg' => '失败！']);
            }
        }
    }
    public function news_del(){
        $param = input('post.');
        $res= Db::name('news')->where(array('id'=>$param['id']))->update(['state'=>3]);
        return json(['code' => 200,'msg' => '删除成功']);

    }
    public function news_state_off(){
        $id=input('param.id');
        $status = Db::name('news')->where(array('id'=>$id))->update(['state'=>2]);
        if($status){
            return json(['code' => 200,'msg' => '已禁止']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function news_state_on(){
        $id=input('param.id');
        $status = Db::name('news')->where(array('id'=>$id))->update(['state'=>1]);
        if($status){
            return json(['code' => 200,'msg' => '已开启']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }

    
}